class Groupacl < ActiveRecord::Base

	def self.paddingAclTable(kontroller, deleteNonExist)
	#kontroller is list(hash type) of action in *_controller.rb in code

		kontroller.each{|ctl,hAct|
			hAct.each{|act, val|
				self.paddingAktion(ctl, act)
			}
		}
		
		if deleteNonExist then
			gacts = Groupaction.find(:all)
			gacts.each{|gact|
				if kontroller[gact.controller].nil?
					gact.delete()
				elsif kontroller[gact.controller][gact.action].nil?
					gact.delete()
				end
			}
		end
	end
	
	def self.paddingAktion(kontroller, aktion)
		gAction = Groupaction.find(:first, :conditions => ["controller = ? AND action = ?", kontroller, aktion])
		if gAction.nil? then
			Groupaction.new(:controller => kontroller, :action => aktion).save
		end
	end
	
	def getHashOfAclByGroup(ctrlHash,groupId)
		groups = Groupacl.find(:first, :conditions=>["group_id = ?", groupId])
		groupAcl = Hash.new()
		
		keys = ctrlHash.keys
		for x in 0..keys.length-1
			aktionHash = Hash.new()
			ctlName = keys[x]
			groupactions = Groupactions.find(:all, :conditions=>["controller = ?", ctlName])
			arrAktions = ctrlHash[ctlName]
			arrAktions.each{|akt|
				actId = getActionId(groupactions, ctlName, akt)
				aktionHash[akt] = self.getPermission(groups,gropuactions, actId)
			}
			groupAcl[ctlKey] = aktionHash
		end
	end
	
	def self.getActionId(gactions, ctl, act)
		gactions.each{|gaction|
			if gaction.controller == ctl and gaction.action == act then
				return gaction.id
			end
		}
		return false
	end
	def self.getGrpPermission(gacls, actId)
		gacls.each{|gacl|
			if gacl.action_id == actId
				return true if gacl.permission == 1
				return false
			end
		}
		return false
	end
	def self.getGroupAcl(lhKontrols, groupId)
		gactions = Groupaction.find(:all)
		gacls = Groupacl.find(:all, :conditions =>["group_id = ?", groupId])
		lhKontrols.each{|ctl,hakt|
			hakt.each{|akt,val|
				actId = self.getActionId(gactions, ctl, akt)
				lhKontrols[ctl][akt] = self.getGrpPermission(gacls, actId)
			}
		}
	end
  
	def self.updateDb(gId,lhKontrols, newAcl)
		gactions = Groupaction.find(:all)
		lhKontrols.each{|ctl,hact|
			hact.each{|act,perm|
				#print  "<groupacl.updateDb>    ctl=", ctl, "    act=",act,"    perm=", perm, "\n"
				actId = self.getActionId(gactions, ctl, act)
				if newAcl.nil?
					if lhKontrols[ctl][act] == true then
						self.writeToAcl(gId, actId, false)
					end
				else
					if newAcl[ctl].nil?
						if lhKontrols[ctl][act] == true then
							self.writeToAcl(gId, actId, false)
						end
					else
						if (newAcl[ctl][act].nil?) then
							if(lhKontrols[ctl][act] == true) then
								self.writeToAcl(gId, actId, false)
							end
						elsif (!newAcl[ctl][act].nil?) and (lhKontrols[ctl][act] == false) then
							self.writeToAcl(gId, actId, true)
						end
					end
				end
			}
		}

	end
	def self.writeToAcl(groupId,actionId,perm)
		acl = Groupacl.find(:first, :conditions => ["group_id = ? AND action_id = ?", groupId, actionId])
		if acl.nil?
			acl = Groupacl.new()
			acl.group_id = groupId
			acl.action_id = actionId
			acl.permission = perm
			acl.save()
		else
			if perm == false
			end
			acl.permission = perm
			acl.save
		end
	end
end
